package com.example.controller;

import cn.hutool.core.date.DateUtil;
import com.example.common.Result;
import com.example.dao.BookDao;
import com.example.dao.OtherDao;
import com.example.entity.*;
import com.example.service.ReserveService;
import com.example.service.ReservebookService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


/**
 * 日期：2025/5/11 17:55
 */

@CrossOrigin
@RestController
@RequestMapping("/reservebook")
public class ReserveBookController {

    @Resource
    private ReservebookService reservebookService;

    @Resource
    private BookDao bookDao;

    @GetMapping("/search")
    public Result findBySearch(Params params){
        PageInfo<Reservebook> info = reservebookService.findBySearch(params);
        return Result.success(info);
    }

    // 新增或修改
    @PostMapping
    public Result save(@RequestBody Reservebook reservebook){
        Book book = bookDao.selectByPrimaryKey(reservebook.getBookId());

        if (book.getNum() == 0){
            return Result.error("数量不能为0");
        }

        reservebook.setTime(DateUtil.now());
        reservebookService.add(reservebook);

        book.setNum(book.getNum() - 1);
        bookDao.updateByPrimaryKey(book);

        return Result.success();
    }



    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        reservebookService.delete(id);
        return Result.success();
    }
}
